<?php
namespace App\Domain;
use App\Model\Im as Model_Im;
use App\Model\User as Model_User;
use App\Domain\Vip as Domain_Vip;

class Im {

    /* 系统通知 */
	public function getSysNotice($uid,$p) {

        $model = new Model_Im();
        $list= $model->getSysNotice($uid,$p);

        foreach($list as $k=>$v){
            unset($v['id']);
            unset($v['touid']);
            unset($v['ip']);
            $v['addtime']=date("Y-m-d H:i:s",$v['addtime']);
            $lang=\PhalApi\DI()->lang;
			if($lang=='en'){
				$v['content']=$v['content_en'];
			}
            $list[$k]=$v;
        }
		return $list;
	}

    /* IM检测 */
	public function check($uid,$touid) {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $isblack=\App\isBlack($uid,$touid);
        if($isblack){
            $rs['code'] = 1001;
			$rs['msg'] = \PhalApi\T('你已把对方拉黑，请先解除拉黑');
			return $rs; 
        }
        $isblack=\App\isBlack($touid,$uid);
        if($isblack){
            $rs['code'] = 1002;
			$rs['msg'] = \PhalApi\T('对方已把你拉黑');
			return $rs; 
        }
        
        
        /* VIP */
        $domain_vip = new Domain_Vip();
        $vipinfo=$domain_vip->myVip($uid);
        
        if($vipinfo['isvip']==1){
            return $rs;
        }
            
        
        $configpub=\App\getConfigPub();
        $configpri=\App\getConfigPri();
        
        $im_limit=$configpri['im_limit'];
        $im_coin=$configpri['im_coin'];
        
        if($im_limit>0){
            
            $date = date("Ymd");
            
            $model = new Model_Im();
            $info= $model->getlimit($uid);
            if(!$info){
                /* 无记录 */
                $data=array(
                    "uid" => $uid,
                    "addtime" => $date,
                    "times" => 1,
                );
                $model->setlimit($data);

            }else if( $date == $info['addtime'] && $info['times'] >= $im_limit){

                // 已达上限
                $rs['code'] = 900; //固定
                $rs['msg'] = \PhalApi\T('此条消息需支付{im_coin}才可发送开通会员可无限畅聊',['im_coin'=>$im_coin.$configpub['name_coin']]);

                $model_user=new Model_User();
                $checkTeenager=$model_user->checkTeenager($uid);

                //开启了青少年模式
                if($checkTeenager['info'][0]['status'] ==1){

                    $rs['code'] = 901; //固定
                    $rs['msg'] = \PhalApi\T('互动聊天每日免费次数已用完'); 
                }

                return $rs;
            }else if($date == $info['addtime']){
                /* 添加次数 */
                $model->addlimit($uid);
            }else{
                /* 已失效 */
                $data=array(
                    "uid" => $uid,
                    "addtime" => $date,
                    "times" => 1,
                );
                $model->uplimit($uid,$data);
            }

        }


		return $rs;
	}

    /* 购买检测 */
	public function buyIm($uid) {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        
        /* VIP */
        $domain_vip = new Domain_Vip();
        $vipinfo=$domain_vip->myVip($uid);
        
        if($vipinfo['isvip']==1){
            //$rs['code'] = 1001;
			//$rs['msg'] = '您已经是VIP，可无限畅聊';
			return $rs; 
        }
        
        $configpri=\App\getConfigPri();

        $im_coin=$configpri['im_coin'];
        
        if($im_coin>0){
            
            /* 扣费 */
            $total=$im_coin;
            $addtime=time();
            $type='0';
            $action='7';
            
            
            /* 更新用户余额 消费 */
            $ifok =\App\upCoin($uid,$total);
            if(!$ifok){
                $rs['code'] = 1003;
                $rs['msg'] = \PhalApi\T('余额不足');
                return $rs;
            }
            
            $insert=array(
                "type"=>$type,
                "action"=>$action,
                "uid"=>$uid,
                "touid"=>$uid,
                "actionid"=>0,
                "nums"=>1,
                "totalcoin"=>$total,
                "addtime"=>$addtime 
            );
            
            \App\addCoinRecord($insert);
            
        }

		return $rs;
	}
	
}
